#定义网页选项部分
area=['办公','生产']
nan1pro=['个人化','印刷','质检','数据室','机修']
hua1pro=['个人化','印刷','质检','数据室','机修']
work=['账号','打印机','系统','软件','VPN','WIFI','外网']

#以下为fastapi设定， 勿动，谢谢
import uvicorn
from fastapi import Cookie, FastAPI
from starlette.responses import Response
import datetime
from starlette.responses import HTMLResponse
from starlette.responses import RedirectResponse
import time
from typing import Optional

#获取变量名称
def var_name(item):
    vars=list(filter(lambda x:"_"not in x,globals().keys()))
    
    for name in vars:
        if item is globals()[name]:
            return name
#print(var_name(nan1_pro))
def test_web(set0,showname):
    z=''
    for item in set0:
        z=z+'<input type="radio" name={} value={}>{}'.format(var_name(set0),item,item)
    
    return '<li class="list-group-item list-group-item-info" style="height:50px">'+showname+z+'</li>'       

def nan1_web(set0):
    z=''
    for item in set0:
        z=z+'<input type="radio" name="department" value={}>{}'.format(item,item)
    return '<li class="list-group-item list-group-item-success" style="height:50px">南昌生产:'+z+'</li>'
def hua1_web(set0):
    z=''
    for item in set0:
        z=z+'<input type="radio" name="department" value={}>{}'.format(item,item)
    return '<li class="list-group-item list-group-item-success" style="height:50px">黄石生产:'+z+'</li>'


web_default='''
<!doctype html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <title>SN助手</title>

    <!-- Bootstrap -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">

    <!-- HTML5 shim 和 Respond.js 是为了让 IE8 支持 HTML5 元素和媒体查询（media queries）功能 -->
    <!-- 警告：通过 file:// 协议（就是直接将 html 页面拖拽到浏览器中）访问页面时 Respond.js 不起作用 -->
    <!--[if lt IE 9]>
      <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
      <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
    <![endif]-->
  </head>
  <body>
 
    <div class="row">
      <div class="col-md-2"></div>      <div class="col-md-8">{web}</div>      <div class="col-md-2"></div>
    </div>
   
    <!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery，所以必须放在前边) -->
    <script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js" integrity="sha384-nvAa0+6Qg9clwYCGGPpDQLVpLNn0fRaROjHqs13t4Ggj3Ez50XnGQqc/r8MhnRDZ" crossorigin="anonymous"></script>
    <!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js" integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd" crossorigin="anonymous"></script>
    
  </body>
</html>
 
'''
web_index='''
<form action='/action'>
{}
<input type="submit" class="btn btn-default" type="button" value="生成前缀码">

</form>
'''

script='''
<script type="text/javascript">
function openlink(){
   window.open('https://itserviceportalgd.service-now.com/incident.do?sys_id=-1&sys_is_list=true&sys_target=incident&sysparm_checked_items=&sysparm_fixed_query=&sysparm_group_sort=&sysparm_list_css=&sysparm_query=active%3dtrue%5eassignment_group%3djavascript%3agetMyGroupsNames()%5eparent_incidentISEMPTY&sysparm_referring_url=incident_list.do%3fsysparm_query%3dactive%253Dtrue%255Eassignment_group%253Djavascript%253AgetMyGroupsNames%2528%2529%255Eparent_incidentISEMPTY%255EORDERBYDESCopened_at%4099%40sysparm_list_mode%3dgrid&sysparm_target=&sysparm_view=&sysparm_view_forced=true')
}
function clip_text(a_string){

    var input = document.createElement('input')
    input.id="__copyText__";
    input.value = a_string; // OOPS! document.getElementById(divId).innerText;
    document.body.appendChild(input);
    input.select();
    document.execCommand("copy");
    var txt = input.value
    input.remove()
    console.log("OK COPIED: '"+txt+"'")

}

function clip_div(divId){
   
   clip_text(document.getElementById(divId).innerText);
   openlink();
}

   
</script>

'''
web_action='''
<br><h3><div id="copyme">{q}</div></h3></br>


<button type="button" class="btn btn-info"><a href="#" onClick = "clip_div('copyme')">新建</a></a></button>
<button type="button" class="btn btn-info"><a href="http://www.bing.com">关闭</a></button>
<button type="button" class="btn btn-info"><a href="http://www.bing.com">我的</a></button>
<button type="button" class="btn btn-info"><a href="http://www.bing.com">团队</a></button>
<button type="button" class="btn btn-info"><a href="/maintain">管理</a></button>
{script}

'''
web_content=test_web(area,'城市:')+nan1_web(nan1pro)+hua1_web(hua1pro)+test_web(work,'工作内容:')

app = FastAPI()
@app.get("/")
def read_root(area: Optional[str] = None,work: Optional[str] = None,department: Optional[str] = '办公'):
    q=''
    return HTMLResponse(content=web_default.format(web=web_index.format(web_content)+web_action.format(q=q,script=script)))

@app.get("/action")
def action(area: Optional[str] = None,work: Optional[str] = None,department: Optional[str] = '办公'):

    q=area+'|'+department+'|'+work+'|'
    return HTMLResponse(content=web_default.format(web=web_index.format(web_content)+web_action.format(q=q,script=script)))

#维护网页
@app.get("/maintain")
async def root(q: str=None):
    content='''

<body>
<h4>请输入维护密码</h4>
<form name="input" action="" method="get">
<input type="text" name="q">
<input type="submit" value="密码">
</form>

</body>

    '''
    if q =='xxxx':
        return RedirectResponse(url='www.baidu.com')
    else:
        return HTMLResponse(content=content)
if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0",port=8083,log_level="info")
